/* eslint-disable no-undefined */
const CopyWebpackPlugin = require('copy-webpack-plugin')
const TerserPlugin = require("terser-webpack-plugin");
const CompressionWebpackPlugin = require("compression-webpack-plugin");
const path = require("path");
const resolve = (dir) => path.join(__dirname, dir);
const isProduction = ["production"].includes(process.env.NODE_ENV);
const productionGzipExtensions = /\.(js|css|json|txt|html|ico|svg)(\?.*)?$/i;
module.exports = {
  configureWebpack: {
    plugins: [
      new CopyWebpackPlugin([
        {
          from: 'node_modules/@easydarwin/easyplayer/dist/component/EasyPlayer.swf',
          to: './libs/EasyPlayer/'
        },
        {
          from: 'node_modules/@easydarwin/easyplayer/dist/component/crossdomain.xml',
          to: './libs/EasyPlayer/'
        },
        {
          from: 'node_modules/@easydarwin/easyplayer/dist/component/EasyPlayer-lib.min.js',
          to: './libs/EasyPlayer/'
        }
      ])
    ]
  },
  // 基本路径
  // publicPath: "/appoint_v2", // vue-cli3.3+新版本使用
  publicPath: process.env.VUE_APP_publicPath || "/appoint_v2", // vue-cli3.3+新版本使用
  // 输出文件目录
  outputDir: "dist",
  // eslint-loader 是否在保存的时候检查
  // false：关闭每次保存都进行检测
  // true：开启每次保存都进行检测，效果与warning一样
  // ‘warning’：开启每次保存都进行检测，lint 错误将显示到控制台命令行，而且编译并不会失败。
  // ‘error’：开启每次保存都进行检测，lint 错误将显示到浏览器页面上，且编译失败。
  // ‘default’：同’error’
  lintOnSave: false,
  // 放置生成的静态资源 (js、css、img、fonts) 的 (相对于 outputDir 的) 目录。
  assetsDir: "static",
  indexPath: "index.html",
  // 以多页模式构建应用程序。
  pages: undefined,
  // 是否使用包含运行时编译器的 Vue 构建版本
  runtimeCompiler: true,
  // 默认babel-loader忽略mode_modules，这里可增加例外的依赖包名
  transpileDependencies: [],
  // 是否为 Babel 或 TypeScript 使用 thread-loader。该选项在系统的 CPU 有多于一个内核时自动启用，仅作用于生产构建，在适当的时候开启几个子进程去并发的执行压缩
  parallel: require("os").cpus().length > 1,
  // 生产环境是否生成 sourceMap 文件，一般情况不建议打开
  productionSourceMap: false,
  // webpack配置
  // 对内部的 webpack 配置进行更细粒度的修改 https://github.com/neutrinojs/webpack-chain see https://github.com/vuejs/vue-cli/blob/dev/docs/webpack.md
  chainWebpack: (config) => {
    config.plugin("html").tap((args) => {
      args[0].title = process.env.VUE_APP_SYS_NAME || "";
      return args;
    });
    /**
     * 删除懒加载模块的prefetch，降低带宽压力
     * https://cli.vuejs.org/zh/guide/html-and-static-assets.html#prefetch
     * 而且预渲染时生成的prefetch标签是modern版本的，低版本浏览器是不需要的
     */
    // config.plugins.delete('prefetch');
    // 添加别名
    config.resolve.alias.set("@", resolve("src"));
  },
  // 调整 webpack 配置 https://cli.vuejs.org/zh/guide/webpack.html#%E7%AE%80%E5%8D%95%E7%9A%84%E9%85%8D%E7%BD%AE%E6%96%B9%E5%BC%8F
  configureWebpack: (config) => {
    // cdn引用时配置externals
    // config.externals = {
    //     'vue': 'Vue',
    //     'element-ui': 'ELEMENT',
    //     'vue-router': 'VueRouter',
    //     'vuex': 'Vuex',
    //     'axios': 'axios'
    // }
    // config.module.rules.push({
    //     test: /\.swf$/,
    //     use: [{
    //         loader: 'url-loader',
    //         options: {
    //             limit: 1024,
    //             // name: 'file/[path][name].[hash:7].[ext]'
    //         }
    //     }]
    // })
    const plugins = [];
    if (isProduction) {
      // 移除console
      plugins.push(
        new TerserPlugin({
          terserOptions: {
            compress: {
              // eslint-disable-next-line camelcase
              pure_funcs: ["console.log"]
            }
          }
        })
      );
      // gzip压缩
      plugins.push(
        new CompressionWebpackPlugin({
          filename: "[path].gz[query]",
          algorithm: "gzip",
          test: productionGzipExtensions,
          threshold: 10240,
          minRatio: 0.8
        })
      );
      config.plugins = [...config.plugins, ...plugins];
    } else {
      // config.devtool = 'cheap-module-eval-source-map';
    }
  },
  css: {
    // 启用 CSS modules
    // modules: false,
    // requireModuleExtensNion: true,
    // 是否使用css分离插件
    extract: isProduction,
    // 开启 CSS source maps，一般不建议开启
    sourceMap: false,
    // css预设器配置项
    loaderOptions: {
      sass: {
        // 设置css中引用文件的路径，引入通用使用的scss文件（如包含的@mixin）
        // data: `
        //             @import '@/assets/styles/base/var.scss';
        //             @import '@/assets/styles/base/mixin.scss';
        //             @import '@/assets/styles/theme/theme.scss';
        //         `
        prependData: `
                      @import '@/assets/styles/base/var.scss';
                      @import '@/assets/styles/base/mixin.scss';
                      @import '@/assets/styles/theme/theme.scss';
                  `
      }
    }
  },

  // webpack-dev-server 相关配置 https://webpack.js.org/configuration/dev-server/
  devServer: {
    host: "0.0.0.0",
    port: 8092, // 端口号
    https: false, // https:{type:Boolean}
    open: false, // 配置自动启动浏览器
    hotOnly: true, // 热更新
    proxy: {
      "/mock": {
        target: "http://rap2api.taobao.org/app/mock/283006/",
        secure: false,
        changeOrigin: true,
        pathRewrite: {
          "^/mock": "/"
        }
      },

      // '/gdw/stomp': {
      //   // target: target, //target为目标变量
      //   target: process.env.VUE_APP_IP || "http://192.168.124.19:8880/gdw/stomp", //target为目标变量 VUE_APP_API
      //   ws: true,
      //   pathRewrite: {
      //     '^/gdw/stomp': '/'
      //   },
      // "/gdw/platform/": {
      //   target: process.env.VUE_APP_IP || "http://10.2.102.138:8880/witness/", // 志磊环境
      //   ws: true,
      //   pathRewrite: {
      //     "^/gdw/platform": "/"
      //   }
      // },
      '/gtv/platform/': {
        // target: target, //target为目标变量
        // target: process.env.VUE_APP_IP || "http://10.2.102.138:8880/witness/",　　//target为目标变量
        // target: process.env.VUE_APP_IP || "http://9527gsxrbapi.wisdomsoft.net:8881/witness/",　　//target为目标变量
        // target: process.env.VUE_APP_IP || "http://localhost:8880/witness/",
        // target: process.env.VUE_APP_IP || "http://192.168.124.19:8880/witness/",
        // target: process.env.VUE_APP_IP || "http://192.168.124.115:8880/witness",// 12
        // target: process.env.VUE_APP_IP || "http://172.16.2.250:8880/witness/",   //生产
        // target: process.env.VUE_APP_IP || "http://192.168.0.8:18880/witness",
        target: process.env.VUE_APP_IP || "http://192.168.102.8:8880/witness",
        ws: true,
        pathRewrite: {
          '^/gtv/platform': '/'
        },
      },
      '/gtv/netty/': {
        // target: target, //target为目标变量
        // target: process.env.VUE_APP_IP || "http://10.2.102.138:8090/witness/gdw/netty/",　　//target为目标变量
        // target: process.env.VUE_APP_IP || "http://9527gsxrbapi.wisdomsoft.net:8881/witness/",　　//target为目标变量
        // target: process.env.VUE_APP_IP || "http://localhost:8880/witness/",
        // target: process.env.VUE_APP_IP || "http://192.168.124.19:8880/witness/",
        // target: process.env.VUE_APP_IP || "http://172.16.2.250:8880/witness/",   //生产
        // target: process.env.VUE_APP_IP || "http://192.168.124.115:8880/witness",   // 12
        // target: process.env.VUE_APP_IP || "http://192.168.0.8:18880/witness",
        target: process.env.VUE_APP_IP || "http://192.168.102.8:8880/witness",
        ws: true,
        pathRewrite: {
          '^/gtv/netty': '/'
        },
      },
      "/gtv/": {
        // process.env.VUE_APP_IP = 'http://10.0.204.143/gtv/'
        // target: process.env.VUE_APP_IP || "http://10.0.204.143:18880/gtv/",
        // target: process.env.VUE_APP_IP || "http://9527gsxrbapi.wisdomsoft.net:8881/witness/",
        // target: process.env.VUE_APP_IP || "http://localhost:8880/witness/",
        // target: process.env.VUE_APP_IP || "http://192.168.124.19:8880/witness/",
        // target: process.env.VUE_APP_IP || "http://172.16.2.250:8880/witness/",   //生产
        // target: process.env.VUE_APP_IP || "http://192.168.124.115:8880/witness",   // 12
         target: process.env.VUE_APP_IP || "http://192.168.102.8:8880/witness",
        secure: false,
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          "^/gtv/": "/gdw/"
        }
      },
      // http://111.198.60.230:9501/witness/gdw/stomp
      //
    }
  },
  // 第三方插件配置 https://www.npmjs.com/package/vue-cli-plugin-style-resources-loader
  pluginOptions: {}
};
